<%@ page import="net.sf.json.JSONObject"%>
<%@ page import="Request.Send"%>
<%@ page import="Main.StartMain"%>
<%@ page import="java.util.Map"%>
<%@ page import="Mysql.Query"%><%@ page import="Mysql.Update"%><%@ page import="Business.FinalAll"%>
<%@ page contentType="application/json;charset=UTF-8"%>
<%
Object code = request.getAttribute("code");
boolean codeTrue = code != null && code.toString().matches("[0-9a-zA-Z]{10,}");//code存在参数条件
if(!codeTrue){//不正确
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("errMsg","参数异常！");
    out.println(jsonObject);
    return;
}
//参数正常
Map<String , Object> baseConfig = StartMain.getConfigData("config");
String AppId = baseConfig.get("AppID").toString();
String AppSecret = baseConfig.get("AppSecret").toString();
Send http = new Request.Send("https://api.weixin.qq.com/sns/jscode2session?" +
 "appid=" + AppId + "&" +
  "secret=" + AppSecret + "&" +
   "js_code=" + code + "&" +
    "grant_type=authorization_code");
String text = http.doGet();
http.disconnect();//关闭连接
boolean textTrue = text != null && text.length() > 3;//text内容条件
if (!textTrue){//不正确
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("errMsg","运行时异常！");
    out.println(jsonObject);
    return;
}
JSONObject textJson;
try{
    textJson = JSONObject.fromObject(text);
}catch(Exception e){
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("errMsg","运行时异常2！");
    out.println(jsonObject);
    return;
}
Object session_keyObj = textJson.get("session_key");
Object openidObj = textJson.get("openid");
boolean textJsonObjTrue = session_keyObj != null && openidObj != null;
if (!textJsonObjTrue){
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("errMsg","运行时异常6！");
    out.println(jsonObject);
    return;
}
String session_key = session_keyObj.toString();
String openid = openidObj.toString();
boolean textJsonTrue = session_key.length() > 3 && openid.length() > 3;
if (!textJsonTrue){
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("errMsg","运行时异常3！");
    out.println(jsonObject);
    return;
}
//更新数据库
Map<String , Object> userinfo = Query.getUser(openid);
boolean hasUser = userinfo != null && userinfo.size() > 0;
String cookie;
if (hasUser){//有用户
    cookie = Update.updCookie(openid);
    boolean cookieErr = cookie == null || cookie.length() <= 10;
    if (cookieErr){//cookie 异常
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errMsg","运行时异常4！");
        out.println(jsonObject);
        return;
    }
}else {//没有用户
    cookie = Business.Cookie.getCookie();
    if (!Update.addUser(openid , cookie)){//如果返回错误
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errMsg","运行时异常5！");
        out.println(jsonObject);
        return;
    }
}//数据库更新
Business.Cookie.removeCookieAll(request.getCookies() , response);//清除所有cookie
Cookie cookie1 = new Cookie("cookie",cookie);
cookie1.setMaxAge(60 * 60 * 24 * FinalAll.OutTimeCookieDay);
cookie1.setPath("/");
response.addCookie(cookie1);//返回cookie
JSONObject reJson = new JSONObject();
reJson.put("errMsg",0);
reJson.put("openid" , openid);
out.println(reJson);
%>
